#!/bin/bash

start_time=$(date +%s)

default_IFS=$IFS

repeat() {
    while :
    do
        $@ && return
        sleep 5
    done
}


# 会判断错误
is_exist_module() {
    submodules=$(git submodule status | awk '{print $2}' | cat)

    IFS=$default_IFS
    for module in $submodules
    do
        if [[ $module == $1 ]];then
            return 1
        fi
    done
    return 0
}

add_repositories() {
    for info in $2
    do
        start=$(date +%s)
        
        index=0
        IFS=";"
        for info_item in $info
        do
            [ $index -eq 0 ] && name=$info_item
            [ $index -eq 1 ] && github_url=$info_item
            let index++
        done
    
        if [ ! -d $1/$name ];then
            repeat git submodule add $github_url $1/$name
        else
            repeat git submodule update $1/$name
        fi
        cp -r $1/$name ~/.vim/plugged/
       
        end=$(date +%s)
        echo "添加子模块 $name 执行时间 => $((end-start))" >> time.log
        #break
    done
}

cp -r .vim ~/

if [ "$1" == "repositories" ];then
    repositories=$(cat repositories | grep -v "#")
    add_repositories "plugins" "${repositories[*]}"
elif [ "$1" == "colorscheme_repositories" ];then
    colorschemes=$(cat colorscheme_repositories | grep -v "#")
    add_repositories "vimcolorschemes" "${colorschemes[*]}"
elif [ "$1" == "plugin_repositories" ];then
    plugins=$(cat plugin_repositories | grep -v "#")
    add_repositories "vimplugins" "${plugins[*]}"
else
    echo "指定参数,repositories,colorscheme_repositories,plugin_repositories!"
fi

end_time=$(date +%s)
echo "执行时间 => $((end-start))" >> time.log

